package com.swsc.solarverse.service.sys.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swsc.solarverse.common.enums.OrganTypeEnums;
import com.swsc.solarverse.common.enums.StateEnums;
import com.swsc.solarverse.common.util.BizAssert;
import com.swsc.solarverse.dao.entity.sys.CorporationDO;
import com.swsc.solarverse.dao.mapper.sys.CorporationMapper;
import com.swsc.solarverse.model.CorporationQueryBO;
import com.swsc.solarverse.service.sys.ICorporationService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 组织机构表 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2022-02-24
 */
@Service
public class CorporationServiceImpl extends ServiceImpl<CorporationMapper, CorporationDO> implements ICorporationService {

    @Override
    public List<CorporationDO> listUserCorporations(Long userNo, String organType) {
        CorporationQueryBO queryBo = CorporationQueryBO.builder()
                .userNo(userNo)
                .organType(organType)
                .state(StateEnums.ENABLE.getEnable())
                .build();
        return baseMapper.listUserCorporations(queryBo);
    }

    @Override
    public List<CorporationDO> listRoleCorporations(Integer tenantNo, Long userNo, Long roleId, Collection<Long> roleIdIn) {
        CorporationQueryBO queryBo = CorporationQueryBO.builder()
                .userNo(userNo)
                .roleId(roleId)
                .tenantNo(tenantNo)
                .roleIdIn(roleIdIn)
                .state(StateEnums.ENABLE.getEnable())
                .build();
        return baseMapper.listRoleCorporations(queryBo);
    }

    @Override
    public CorporationDO getByOrganNo(Integer organNo) {
        return baseMapper.selectOne(Wrappers.lambdaQuery(CorporationDO.class).eq(CorporationDO::getOrganNo, organNo));
    }

    @Override
    public CorporationDO getByTenantOrganNo(Integer tenantNo, Integer organNo) {
        return baseMapper.selectOne(Wrappers.lambdaQuery(CorporationDO.class).eq(CorporationDO::getTenantNo, tenantNo).eq(CorporationDO::getOrganNo, organNo));
    }

    @Override
    public List<CorporationDO> getChildrenByTenantOrganPno(Integer tenantNo, Integer organPno) {
        return baseMapper.selectList(Wrappers.lambdaQuery(CorporationDO.class)
                .eq(CorporationDO::getTenantNo, tenantNo)
                .eq(CorporationDO::getOrganPno, organPno));

    }

    @Override
    public CorporationDO getStationByOrganNo(Integer organNo) {
        return baseMapper.selectOne(Wrappers.lambdaQuery(CorporationDO.class).eq(CorporationDO::getOrganNo, organNo)/**.eq(CorporationDO::getOrganType, OrganTypeEnums.PS.getType())*/.in(CorporationDO::getOrganType,OrganTypeEnums.PS.getType(),OrganTypeEnums.BO.getType()));
    }

    @Override
    public CorporationDO checkStationByOrganNo(Integer organNo) {
        CorporationDO corporationDo = getStationByOrganNo(organNo);
        BizAssert.isFalse(null == corporationDo, "电站不存在");
        return corporationDo;
    }

    @Override
    public CorporationDO checkStationByTanantNoOrganNo(Integer tenantNo, Integer organNo) {
        CorporationDO rawDo = getByTenantOrganNo(tenantNo, organNo);
        BizAssert.isFalse(null == rawDo || !OrganTypeEnums.PS.getType().equals(rawDo.getOrganType()), "电站不存在");
        return rawDo;
    }

    @Override
    public List<CorporationDO> listCorpWithSub(Integer organNo) {
        return baseMapper.listCorpWithSub(organNo);
    }
}
